<template>
  <div class="panel">
    <div ref="chartRef" class="chart"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'

const chartRef = ref<HTMLElement | null>(null)
let chart: echarts.ECharts | null = null

const initChart = () => {
  if (!chartRef.value) return
  
  chart = echarts.init(chartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      }
    },
    legend: {
      data: ['A区', 'B区', 'C区'],
      textStyle: {
        color: '#33ffd0',
        fontSize: 10
      },
      right: 'center',
      top: '1%',
      itemGap: 8
    
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: '15%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: Array.from({length: 25}, (_, i) => `${i}:00`),
      axisLine: {
        lineStyle: {
          color: '#33ffd0'
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: '#33ffd0',
        formatter: (value: string) => value.split(':')[0]
      },
      name: '时间',
      nameTextStyle: {
        color: '#33ffd0',
        padding: [0, 0, 0, 0]
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: '#33ffd0',
        formatter: '{value}%'
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(51, 255, 208, 0.1)'
        }
      },
      name: '湿度(%)',
      nameTextStyle: {
        color: '#33ffd0',
        padding: [0, 0, 0, 0]
      }
    },
    series: [
      {
        name: 'A区',
        type: 'line',
        smooth: true,
        data: [45, 52, 48, 65, 58, 72, 68, 85, 78, 92, 88, 95, 82, 75, 68, 72, 65, 58, 52, 48, 42, 38, 45, 52, 48],
        itemStyle: {
          color: '#33ffd0'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(51, 255, 208, 0.3)' },
            { offset: 1, color: 'rgba(51, 255, 208, 0.1)' }
          ])
        }
      },
      {
        name: 'B区',
        type: 'line',
        smooth: true,
        data: [62, 58, 65, 72, 68, 82, 78, 92, 85, 95, 88, 82, 75, 68, 72, 65, 58, 52, 48, 42, 38, 45, 52, 58, 62],
        itemStyle: {
          color: '#F56C6C'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(245, 108, 108, 0.3)' },
            { offset: 1, color: 'rgba(245, 108, 108, 0.1)' }
          ])
        }
      },
      {
        name: 'C区',
        type: 'line',
        smooth: true,
        data: [38, 45, 52, 48, 65, 58, 72, 68, 85, 78, 92, 88, 95, 82, 75, 68, 72, 65, 58, 52, 48, 42, 38, 45, 38],
        itemStyle: {
          color: '#E6A23C'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(230, 162, 60, 0.3)' },
            { offset: 1, color: 'rgba(230, 162, 60, 0.1)' }
          ])
        }
      }
    ]
  }
  
  chart.setOption(option)
}

// 监听窗口大小变化
const handleResize = () => {
  chart?.resize()
}

onMounted(() => {
  initChart()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  chart?.dispose()
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.panel {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chart {
  width: 100%;
  height: 100%;
}
</style> 